package com.xh.web.controller;

import java.util.List;

import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DContent;
import com.xh.web.service.IDContentService;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 目录Controller
 *
 * @author dongxuefei
 * @date 2020-09-16
 */
@RestController
@RequestMapping("/db/content")
public class DContentController extends BaseController
{
    @Autowired
    private IDContentService dContentService;

    /**
     * 查询目录列表,模糊查询
     */
    @GetMapping("/selContent")
    public TableDataInfo list(DContent dContent)
    {
        List<DContent> list = dContentService.selectDContentList(dContent);
        return getDataTable(list);
    }


    /**
     * 新增目录
     */
    @PostMapping("/insertContent/{projectId}")
    public AjaxResult add(@PathVariable String projectId, @RequestBody DContent dContent)
    {
        return toAjax(dContentService.insertDContent(projectId,dContent));
    }

    /**
     * 修改目录
     */
    @PostMapping("/updateContent")
    public AjaxResult edit(@RequestBody DContent dContent)
    {
        return toAjax(dContentService.updateDContent(dContent));
    }

    /**
     * 删除目录
     */
    @DeleteMapping("/delContent/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
       List<DContent>  dContents= dContentService.selectDContentByParentId(id);
       if (dContents.size()!=0){
           return AjaxResult.error("该目录下存在子目录，无法删除");
       }
        return toAjax(dContentService.deleteDContentById(id));
    }

}
